<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="维修日期" prop="repairTime" clearable>
        <el-date-picker
          v-model="queryParams.times"
          placeholder="请选择"
          type="datetimerange"
          value-format="timestamp"
          range-separator="~"
          style="width: 200px"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="维修类型" prop="repairType">
        <el-select
          v-model="queryParams.repairType"
          style="width: 110px"
          clearable
        >
          <el-option
            v-for="item in repairTypeList"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆编号" prop="carNumber">
        <el-input
          style="width: 100px"
          v-model="queryParams.carNumber"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌照" prop="carLicensePlate" label-width="54px">
        <el-input
          style="width: 100px"
          v-model="queryParams.carLicensePlate"
          placeholder="请输入车牌照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:repair:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      max-height="410"
      v-loading="loading"
      :data="repairList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="记录id" align="center" prop="id" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="车辆id" align="center" prop="carId" /> -->
      <el-table-column
        label="车辆编号"
        align="center"
        prop="carNumber"
        width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="车牌照"
        align="center"
        prop="carLicensePlate"
        width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="维修类型"
        align="center"
        prop="repairType"
        width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="维修日期"
        align="center"
        prop="repairTime"
        width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.repairTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修厂家" align="center" prop="repairShop" />
      <el-table-column
        label="维修总支出"
        align="center"
        prop="repairCost"
        width="140"
      />
      <el-table-column
        label="最后编辑时间"
        align="center"
        prop="updateTime"
        width="160px"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="支出明细"
        align="center"
        prop="repairCostDetail"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        width="120"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service:repair:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:repair:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆维修记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
        <el-form-item label="车牌照" prop="carLicensePlate">
          <el-select
            v-model="form.carLicensePlate"
            filterable
            style="width: 100%"
            @change="handleCarChange"
          >
            <el-option
              v-for="item in carList"
              :key="item.id"
              :label="item.carLicensePlate"
              :value="item.carLicensePlate"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修日期" prop="repairTime">
          <el-date-picker
            type="date"
            style="width: 100%"
            value-format="timestamp"
            v-model="form.repairTime"
            placeholder="请输入维修日期"
          />
        </el-form-item>
        <el-form-item label="维修类型" prop="repairType">
          <el-select v-model="form.repairType" style="width: 100%">
            <el-option
              v-for="item in repairTypeList"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修厂家" prop="repairShop">
          <el-input v-model="form.repairShop" placeholder="请输入维修厂家" />
        </el-form-item>
        <el-form-item label="维修总支出" prop="repairCost">
          <el-input v-model="form.repairCost" placeholder="请输入修理费" />
        </el-form-item>

        <!-- <el-form-item label="车辆编号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车辆编号" />
        </el-form-item> -->

        <el-form-item label="支出明细" prop="repairCostDetail">
          <el-button type="primary" size="mini" @click="handleDataAdd"
            >新增</el-button
          >
          <el-table :data="dataList" size="mini">
            <el-table-column
              prop="repairItem"
              label="维修项目名称"
              :show-overflow-tooltip="true"
              min-width="200"
            >
              <template slot-scope="scope">
                <el-input
                  v-model.trim="scope.row.repairItem"
                  placeholder="请输入维修项目名称"
                  clearable
                  size="mini"
                >
                </el-input>
              </template>
            </el-table-column>
            <el-table-column
              prop="repairCost"
              label="支出"
              :show-overflow-tooltip="true"
              min-width="200"
            >
              <template slot-scope="scope">
                <el-input
                  v-model.trim="scope.row.repairCost"
                  placeholder="请输入支出金额"
                  clearable
                  size="mini"
                >
                </el-input>
              </template>
            </el-table-column>

            <el-table-column
              width="80"
              label="操作"
              fixed="right"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDataDelete(scope.$index, scope.row)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="上传单据" prop="invoices">
          <file-upload
            :isShowTip="false"
            v-model="form.invoices"
            :fileType="['png', 'jpg', 'jpeg']"
          ></file-upload>
        </el-form-item>
        <template v-if="form.id">
          <el-form-item label="最后编辑时间">{{
            formatDate(form.updateTime)
          }}</el-form-item>
          <el-form-item label="创建时间">{{
            formatDate(form.createTime)
          }}</el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepair,
  getRepair,
  delRepair,
  addRepair,
  updateRepair,
} from "@/api/service/repair";
import { listCars } from "@/api/service/cars";
import { formatDate } from "@/utils";

export default {
  name: "Repair",
  data() {
    return {
      dataList: [],
      carList: [], // che
      repairTypeList: [
        "电器",
        "机修",
        "轮胎",
        "液压系统",
        "定期保养",
        "换季保养",
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆维修记录表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        carNumber: null,
        carLicensePlate: null,
        repairTime: null,
        repairType: null,
        repairShop: null,
        repairCost: null,
        repairCostDetail: null,
        times: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carId: [{ required: true, message: "车辆id不能为空", trigger: "blur" }],
        carNumber: [
          { required: true, message: "车辆编号不能为空", trigger: "blur" },
        ],
        carLicensePlate: [
          { required: true, message: "车牌照不能为空", trigger: "blur" },
        ],
        repairTime: [
          { required: true, message: "维修时间不能为空", trigger: "blur" },
        ],
        repairType: [
          { required: true, message: "维修类型不能为空", trigger: "change" },
        ],
        repairShop: [
          { required: true, message: "维修厂家不能为空", trigger: "blur" },
        ],
        repairCost: [
          { required: true, message: "修理费不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        invoices: [
          { required: true, message: "上传单据不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    open(n, o) {
      if (n) {
        this.$nextTick(() => {
          this.getCarList();
        });
      }
    },
  },
  methods: {
    formatDate,
    handleCarChange(val) {
      this.form.carNumber = this.carList.find(
        (s) => s.carLicensePlate === val
      )?.carNumber;
    },
    // 添加行
    handleDataAdd() {
      const list = {
        repairItem: "",
        repairCost: "",
      };

      this.dataList.push(list);
    },
    // 删除行
    handleDataDelete(index, row) {
      this.dataList.splice(index, 1);
    },
    getCarList() {
      listCars({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.carList = res.rows;
      });
    },
    formatDate,
    /** 查询车辆维修记录列表 */
    getList() {
      this.loading = true;
      listRepair({
        ...this.queryParams,
        startTime: this.queryParams.times?.[0],
        endTime: this.queryParams.times?.[1],
      }).then((response) => {
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        carNumber: null,
        carLicensePlate: null,
        repairTime: null,
        repairType: null,
        repairShop: null,
        repairCost: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        repairCostDetail: null,
        invoices: null,
      };
      this.dataList = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.times = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增车辆维修记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRepair(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆维修记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRepair({
              ...this.form,
              repairCostDetail: this.dataList,
            }).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepair({ ...this.form, repairCostDetail: this.dataList }).then(
              (response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            );
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("确定要删除吗？")
        .then(function () {
          return delRepair(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/repair/export",
        {
          ...this.queryParams,
        },
        `repair_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
